package com.yanqu.road.dao.impl.activity.horserun.game;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.horserun.HorseRunUserBaseData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class HorseRunUserBaseDataDaoImpl extends TempDataObjectDao<HorseRunUserBaseData> {
    public static String getHorseRunUserBaseDataSql = "select * from `t_u_horse_run_activity_user_base` where `activityId`=? and `userId`=?";
    public HorseRunUserBaseData getHorseRunUserBaseData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getHorseRunUserBaseDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateHorseRunUserBaseDataSql = "update `t_u_horse_run_activity_user_base` set horseAppearance=?,pointAttrLevel=?,extAttr=?,totalAttrMap=?,activityGrade=?,equip=?,hasUnlockEquip=?,skill=?,hasOpenGameTime=?,attrPoint=?,horseLevel=?,horseExp=?,hadUseItemNum=?,historyMaxStage=?,bigLuckyTime=?,turnTableRemain=?,refreshAttrTime=?,canPlay=?,lockUnionId=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(HorseRunUserBaseData horseRunUserBaseData) {
        horseRunUserBaseData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserBaseData.getHorseAppearance()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserBaseData.getPointAttrLevel())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserBaseData.getExtAttr())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserBaseData.getTotalAttrMap())));
        para.put(i++, new DbParameter(Types.BIGINT, horseRunUserBaseData.getActivityGrade()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserBaseData.getEquip())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserBaseData.getHasUnlockEquip())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserBaseData.getSkill())));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserBaseData.getHasOpenGameTime()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserBaseData.getAttrPoint()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserBaseData.getHorseLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, horseRunUserBaseData.getHorseExp()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserBaseData.getHadUseItemNum()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserBaseData.getHistoryMaxStage()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserBaseData.getBigLuckyTime()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserBaseData.getTurnTableRemain()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserBaseData.getRefreshAttrTime()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserBaseData.getCanPlay()));
        para.put(i++, new DbParameter(Types.VARCHAR, horseRunUserBaseData.getLockUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserBaseData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, horseRunUserBaseData.getUserId()));
        if (getDb().executeNoneQuery(updateHorseRunUserBaseDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHorseRunUserBaseDataSql = "insert into `t_u_horse_run_activity_user_base`(activityId,userId,horseAppearance,pointAttrLevel,extAttr,totalAttrMap,activityGrade,equip,hasUnlockEquip,skill,hasOpenGameTime,attrPoint,horseLevel,horseExp,hadUseItemNum,historyMaxStage,bigLuckyTime,turnTableRemain,refreshAttrTime,canPlay,lockUnionId) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(HorseRunUserBaseData horseRunUserBaseData) {
        horseRunUserBaseData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserBaseData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, horseRunUserBaseData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserBaseData.getHorseAppearance()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserBaseData.getPointAttrLevel())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserBaseData.getExtAttr())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserBaseData.getTotalAttrMap())));
        para.put(i++, new DbParameter(Types.BIGINT, horseRunUserBaseData.getActivityGrade()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserBaseData.getEquip())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserBaseData.getHasUnlockEquip())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserBaseData.getSkill())));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserBaseData.getHasOpenGameTime()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserBaseData.getAttrPoint()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserBaseData.getHorseLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, horseRunUserBaseData.getHorseExp()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserBaseData.getHadUseItemNum()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserBaseData.getHistoryMaxStage()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserBaseData.getBigLuckyTime()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserBaseData.getTurnTableRemain()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserBaseData.getRefreshAttrTime()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserBaseData.getCanPlay()));
        para.put(i++, new DbParameter(Types.VARCHAR, horseRunUserBaseData.getLockUnionId()));
        if (getDb().executeNoneQuery(insertHorseRunUserBaseDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HorseRunUserBaseData parseData(ResultSet rs) throws SQLException {
        HorseRunUserBaseData horseRunUserBaseData = new HorseRunUserBaseData();
        horseRunUserBaseData.setActivityId(rs.getInt("activityId"));
        horseRunUserBaseData.setUserId(rs.getLong("userId"));
        horseRunUserBaseData.setHorseAppearance(rs.getInt("horseAppearance"));
        horseRunUserBaseData.setPointAttrLevel(JSON.parseObject(rs.getString("pointAttrLevel"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {}));
        horseRunUserBaseData.setExtAttr(JSON.parseObject(rs.getString("extAttr"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {}));
        horseRunUserBaseData.setTotalAttrMap(JSON.parseObject(rs.getString("totalAttrMap"), new TypeReference<ConcurrentHashMap<java.lang.Integer, java.lang.Integer>>() {}));
        horseRunUserBaseData.setActivityGrade(rs.getLong("activityGrade"));
        horseRunUserBaseData.setEquip(JSON.parseObject(rs.getString("equip"), new TypeReference<ConcurrentHashMap<java.lang.Integer, java.lang.Integer>>() {}));
        horseRunUserBaseData.setHasUnlockEquip(JSON.parseObject(rs.getString("hasUnlockEquip"), new TypeReference<java.util.List<java.lang.Integer>>() {}));
        horseRunUserBaseData.setSkill(JSON.parseObject(rs.getString("skill"), new TypeReference<ConcurrentHashMap<java.lang.Integer, com.yanqu.road.entity.activity.horserun.HorseRunSkillData>>() {}));
        horseRunUserBaseData.setHasOpenGameTime(rs.getInt("hasOpenGameTime"));
        horseRunUserBaseData.setAttrPoint(rs.getInt("attrPoint"));
        horseRunUserBaseData.setHorseLevel(rs.getInt("horseLevel"));
        horseRunUserBaseData.setHorseExp(rs.getLong("horseExp"));
        horseRunUserBaseData.setHadUseItemNum(rs.getInt("hadUseItemNum"));
        horseRunUserBaseData.setHistoryMaxStage(rs.getInt("historyMaxStage"));
        horseRunUserBaseData.setBigLuckyTime(rs.getInt("bigLuckyTime"));
        horseRunUserBaseData.setTurnTableRemain(rs.getInt("turnTableRemain"));
        horseRunUserBaseData.setRefreshAttrTime(rs.getInt("refreshAttrTime"));
        horseRunUserBaseData.setCanPlay(rs.getInt("canPlay"));
        horseRunUserBaseData.setLockUnionId(rs.getString("lockUnionId"));

        return horseRunUserBaseData;
    }
}
